<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27/0027
 * Time: 17:18
 */

namespace app\sports_admin\controller;


use think\captcha\Captcha;
use think\Controller;
use app\sports_admin\service\Login as LoginService;
use think\Request;
use think\Session;

class Login extends Controller
{
    /**
     * 登录首页
    */
    public function index()
    {
        return view('index');
    }

    /**
     * 验证码
    */
    public function code()
    {
        $captcha = new Captcha();
        $captcha->fontSize = 30;
        $captcha->length   = 4;
        $captcha->useNoise = false;
        $captcha->expire = 300;
        return $captcha->entry();
    }
    /**
     * 登录验证
    */
    public function checkLogin()
    {
        $params  = Request::instance()->only(['code']);
        if(!captcha_check(trim($params['code']))){
            return json(['errorCode'=>400,'msg'=>'验证码错误']);
        }
        //执行登录验证
        $result = LoginService::checkLogin();
        //设置session
        Session::set('sport_admin',$result);
        return json(['code'=>200,'msg'=>'登录成功']);
    }
    /**
     * 登录退出
    */
    public function loginOut()
    {
        Session::delete('sport_admin');
        $this->redirect('/sport_admin/login');
    }
}